package cn.gdchent.springbootsecurity.service.impl;

import cn.gdchent.springbootsecurity.configbean.SecurityUserDetailsServiceBean;
import cn.gdchent.springbootsecurity.enity.UserInfoDetails;
import cn.gdchent.springbootsecurity.mapper.UserDetailsServiceMapper;
import cn.gdchent.springbootsecurity.mapper.UserMapper;
import cn.gdchent.springbootsecurity.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;

/**
 * @auther:gdchent
 * @create:2020/4/1 11:37
 * @Description:作用是用来认证数据源
 */
@Service(value = "userDetailsServiceImpl")
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserMapper userMapper ;

    @Autowired
    private UserDetailsServiceMapper userDetailsServiceMapper;

    @Autowired
    private DataSource dataSource;

    @Autowired
    private SecurityUserDetailsServiceBean securityUserDetailsServiceBean;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println("loadUserByUserName");
        if(StringUtils.isEmpty(username)){
            throw new NullPointerException("用户不能为空");
        }
        //从数据库获取用户
        UserInfoDetails userInfoDetails =userMapper.findByUserName(username);
        //用户不存在
        if(userInfoDetails ==null){
             throw new NullPointerException("用户不存在");
        }
//        //获取用户角色列表
//        List<String> roleCodes=userDetailsServiceMapper.findRoleByUserName(username);
//
//        //通过角色列表获取权限列表
//        List<String> authorityByRoleCodes=userDetailsServiceMapper.findAuthorityByRoleCodes(roleCodes);
//
//        //为角色标识加上ROLE_前缀
//        roleCodes=roleCodes.stream().map(rc -> "ROLE_"+ rc)
//                .collect(Collectors.toList());
//        //角色是一种特殊的权限，所以合并
//        authorityByRoleCodes.addAll(roleCodes);
        //转成用逗号隔开的字符串，为空户设置权限标识
        userInfoDetails.setAuthorities(AuthorityUtils.commaSeparatedStringToAuthorityList(
                String.join(",","ROLE_USER")
        ));
        return userInfoDetails;
    }

    //自行实现权限的转换
    private List<GrantedAuthority> grantedAuthorities(String roles){
         List<GrantedAuthority> authorities=new ArrayList<>();
         String[] roleArray =roles.split(";");
         if(!"".equals(roles)){
             for(String role:roleArray){
                  authorities.add(new SimpleGrantedAuthority(role));
             }
         }
         return authorities;
    }

}
